package com.billsharing.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class BillReportFriends
 */
@WebServlet("/BillReportFriends")
public class BillReportFriends extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	int uid, i=0;

	double billamt1;
    String result;
    Connection conn;
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		PrintWriter out=response.getWriter();
		String billamt,billdesc,billdate,paidid,sharedid;
		uid = Integer.parseInt(request.getParameter("uid"));
		billamt=request.getParameter("billamt");
		double credit = 0,debit = 0;
		billdesc=request.getParameter("billdesc");
		billdate=request.getParameter("billdate");
		paidid=request.getParameter("paidid");
		sharedid=request.getParameter("sharedids");
		String credit1 = "0",debit1 = "0";
		billamt1=Double.parseDouble(billamt);
		 try
	        {
	            
	            Class.forName ("com.mysql.jdbc.Driver").newInstance();
	            conn = DriverManager.getConnection ("jdbc:mysql://csdb.csc.villanova.edu/auppalap", "auppalap", "fL00355727");
	            System.out.println ("Database connection established");
	            Statement s=conn.createStatement();
	            Statement s1=conn.createStatement();
	            Statement s2=conn.createStatement();
	            User paidemail= new User(paidid);
	            int paidid1=paidemail.getUid();
	            User sharedemail= new User(sharedid);
	            int sharedid1=sharedemail.getUid();
	            int i3 =s.executeUpdate("insert into Bill (billamt,billdesc,paidid,sharedid) values ("+billamt1+",'"+billdesc+"',"+paidid1+","+sharedid1+")");
	        int i1 = 0, i2=0;
	            if(i3>0)
	            {
	            	s2.executeQuery("select credit,debit from Friend where userid="+paidid1);
	            	ResultSet rs=s2.getResultSet();
	            	while(rs.next())
	            	{
	            		
	            	credit1=rs.getString("credit");
	            	debit1= rs.getString("debit");
	            	credit= Double.parseDouble(credit1);
	            	debit= Double.parseDouble(debit1);
	            	System.out.println("Credit is"+ credit);
	            	System.out.println("Debit is"+ debit);
	            	}
	            	double billshared=billamt1/2;
	            	System.out.println("billshared "+billshared);
	            	if(credit==0 && debit==0)
	            	{
	            		System.out.println("Inside intial loop");
	            		i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedid+"'");
	            	 
	            		i2 =s1.executeUpdate("UPDATE Friend SET debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidid+"'");
	            		 
	            	}
	            	else if(credit>=debit)
	            	
	            	{
	            		System.out.println("Inside credit>debit loop");
	            		billshared=billshared+credit;
	            		System.out.println("billshared "+billshared);
	            		i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedid+"'");
	            		 
	            		i2 =s1.executeUpdate("UPDATE Friend SET debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidid+"'");
	            		 
	            	}
	            	
	            	else if(debit>credit)
	            	{ 
	            		System.out.println("Inside debit>credit loop");
	            		 if (debit>=billshared)
	            		 {
	            			 System.out.println("Inside debit>billshared loop");
	            		billshared=debit-billshared;
	            		i1 =s.executeUpdate("UPDATE Friend SET credit="+i+",debit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedid+"'");
	            		 
	            		i2 =s1.executeUpdate("UPDATE Friend SET credit="+billshared+",debit="+i+" where userid="+sharedid1+" and fndemail='"+paidid+"'");
	            		// 
	            		 }
	            		 else
	            		 {
	            			 System.out.println("Inside final loop");
	            			billshared=billshared-debit;
	            			i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+",debit="+i+" where userid="+paidid1+" and fndemail='"+sharedid+"'");
	            			
	            			i2 =s1.executeUpdate("UPDATE Friend SET credit="+i+",debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidid+"'");
	            		
	            		 }
	            		 
	            		 System.out.println(" Credit is"+ credit);
	 	            	System.out.println(" Debit is"+ debit);
	 	            	System.out.println("billshared"+billshared);
	            	}
	            	
	            	if(i1>0)
	            	{
	            		result="true";
	            	}
	            	else
	            	{
	            		result="false";
	            	}
	            	
	            }
	            out.print(result);
	        } 
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
		
		
	}
}


